import { _decorator, Button, Component, director, find, Label, Node, Sprite, SpriteFrame } from 'cc';

import { ResourceManager } from '../Core/ResourceManager';
import { Core } from '../Core/Core';
import { BattleManager } from '../BattleManager';
import { IItem } from './Item';
import { EEvent, ESkill } from '../Config/Enum';
const { ccclass, property } = _decorator;

// 武学格子
@ccclass('SkillItem')
export class SkillItem extends Component {

    item: IItem
    ndSkillItemDetail
    itemList


    skillName:ESkill
    skill
    playerData
    ndSkillDetail:Node
    ndMask:Node
    


    Btn_upgrade:Button

    protected onLoad(): void {
        // this.playerData = Core.Instance.players.get(Core.Instance.currentPlayer);
        // this.skillName = Core.Instance.currentSkill = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
        // this.skill = this.playerData.skillData.skills.get(this.skillName);
        this.ndSkillDetail = find("Canvas/PlayerView/SkillDetail") as Node;
        this.ndMask = this.node.getChildByName("Mask");

        console.log("skillitem onload, 当前武学:", this.skill, this.skillName, "playerType:", Core.Instance.currentPlayer);
        


    }

    protected onEnable(): void {
        console.log("skillitem onenable, 当前武学:", this.skill, this.skillName, "playerType:", Core.Instance.currentPlayer);
        this.playerData = Core.Instance.players.get(Core.Instance.currentPlayer);
        this.skillName = Core.Instance.currentSkill = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
        this.skill = this.playerData.skillData.skills.get(this.skillName);
        this.node.on(Node.EventType.TOUCH_END, this.onBtnOpenSkillDetail, this);
        switch (this.node.name) {
            case "Skill1":
                this.ndMask.active = false; // 符文遮罩
                break;
            case "Skill2":
                if(this.playerData.level >= 3){
                    this.ndMask.active = false; // 符文遮罩
                }else{
                    this.ndMask.active = true; // 符文遮罩
                }
                break;
            case "Skill3":
                if(this.playerData.level >= 5){
                    this.ndMask.active = false; // 符文遮罩
                }else{
                    this.ndMask.active = true; // 符文遮罩
                }
                break;
            default:
                console.error("未知武学类型:", Core.Instance.currentSkill);
        }
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_END, this.onBtnOpenSkillDetail, this);
        
    }

    onBtnOpenSkillDetail(){
        this.skillName = Core.Instance.currentSkill = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
        console.log("点击了武学详情按钮", this.node.name);
        if (this.ndSkillDetail) {
            this.ndSkillDetail.active = true;
        } else {
            console.error("ndSkillDetail节点不存在");
        }

    }


    // onBtnUpgrade() {
    //     this.playerData = Core.Instance.players.get(Core.Instance.currentPlayer);
    //     this.skillName = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
    //     this.skill = this.playerData.skillData.skills.get(this.skillName);
    //     console.log("点击了升级按钮");
    //     console.log("当前武学:", this.skill, "武学名称:", this.skillName, "玩家数据:", this.playerData);

    //     // 检查武学是否可以升级
    //     if (this.playerData.skillPoints <= 0) {
    //         console.error("武学点不足，无法升级武学");
    //         return;
    //     }


    //     // 检查武学是否达到最大等级
    //     console.log("当前武学等级:", this.skill.level, "最大武学等级:", this.skill.maxLevel);
    //     if (this.skill.level >= this.skill.maxLevel) {
    //         console.error("武学已达到最大等级，无法升级");
    //         return;
    //     }

    //     this.playerData.skillPoints -= 1; // 扣除一个武学点
    //     console.log("当前武学点:", this.playerData.skillPoints);

    //     // 这里可以添加升级武学的逻辑
    //     this.playerData.skillData.upgradeSkill(this.skillName);
    //     console.log("升级后的武学数据:", this.playerData.skillData.skills.get(this.skillName));


    //     Core.Instance.event.emit(EEvent.PlayerDataChange) // 通知其他组件更新武学点显示
        

    // }


}


